#!/bin/sh
VERSION=0.1
PACKAGE=salmon_viewer
prefix=/usr/local
config=RELEASE
configurations=" RELEASE DEBUG"
required_packages=""

usage ()
{
	echo "Usage : configure [--prefix=PREFIX] [--config=CONFIG]"
	echo
	echo "Configurations available :"
	for c in $configurations; do
		if [ "$c" = "$config" ]; then
			echo "        $c (Default)"
		else
			echo "        $c"
		fi
	done
}

validate_config ()
{
	test -z "$1" && return 0
	for c in $configurations; do
		if [ "$c" = "$1" ]; then
			return 1
		fi
	done
	return 0
}

check_required_packages ()
{
	echo "Looking for required packages" | tee config.log
	for pkg in $required_packages; do
		name=`echo $pkg | cut -d\; -f1`
		version=`echo $pkg | cut -d\; -f2`

		echo -n "Checking for package '$name'.." | tee -a config.log
		pkg-config --atleast-version=$version $name
		if [ $? -ne 0 ]; then
			echo " ERROR: Package named '$name' >= $version not found." | tee -a config.log
			echo "Try adjusting your PKG_CONFIG_PATH environment variable." | tee -a config.log
			return 1
		fi
		echo " found." | tee -a config.log
	done
	return 0
}

while test x$1 != x; do
        case $1 in
                --prefix=*)
                        prefix=`echo $1 | sed 's/--prefix=//'`
                        ;;
                --prefix)
                        echo --prefix needs an argument: --prefix=directory >&2

                        ;;
		--config=*)
                        conf=`echo $1 | sed 's/--config=//'`
			validate_config "$conf"
			if [ $? -eq 1 ]; then
				config=$conf
			else
				echo "Invalid config name - $conf"
				usage
				exit 1
			fi
                        ;;
		--help)
			usage
			exit
			;;
                *)
                        echo Unknown argument $1 >&2
			usage
			exit 1
			;;
        esac
        shift
done

check_required_packages
[ $? -eq 1 ] && exit 1

echo "prefix=$prefix" > config.make
echo "RUNTIME=mono" >> config.make
echo "ASSEMBLY_VERSION=$VERSION.0.0" >> config.make
echo "VERSION=$VERSION" >> config.make
echo "PACKAGE=$PACKAGE" >> config.make
echo "CONFIG=$config" >> config.make

echo
echo "$PACKAGE has been configured with "
echo "        prefix = $prefix"
echo "        config = $config"
echo
